今天是鐵人賽的第五天,我學習字典的應用。
創建字典
student = {'name': 'John', 'age': 25}
訪問值
name = student['name']
print('學生姓名:', name)
添加或更新鍵值對
student['age'] = 26
student['major'] = 'Computer Science'
print('更新後的學生資訊:', student)
刪除鍵值對
del student['age']
print('刪除年齡後的學生資訊:', student)
遍歷字典
print('遍歷學生資訊:')
for key, value in student.items():
print(key, value)